package wangwenjun.phase2.concurrencypattern.activeobject.cglib.framework;

import java.util.LinkedList;

/**
 * @author ChangLiang
 * @date 2020/6/18
 */
class ActiveObjectMessageQueue {

    private final LinkedList<ActiveObjectMessage> messagesList = new LinkedList<>();

    public ActiveObjectMessageQueue() {
        new ActiveObjectServiceDaemonThread(this).start();
    }

    public void offer(ActiveObjectMessage message) {
        synchronized (this) {
            messagesList.addLast(message);
            this.notify();
        }
    }

    public ActiveObjectMessage take() {
        synchronized (this) {
            while (messagesList.size() <= 0) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return messagesList.removeFirst();
        }
    }
}
